package com.ruoyi.web.controller.erpbase;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.Cars;
import com.ruoyi.system.service.ICarsService;
import org.apache.commons.lang3.ObjectUtils;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * 开发单位：FESCO Adecco
 * 版权：FESCO Adecco
 *
 * @author：hechunbo
 * @since： JDK1.8
 * @version：1.1
 * @date：2025/8/26 最后更新日期：
 * 修改人：hechunbo
 */
public class ExecListener implements ExecutionListener {


    @Override
    public void notify(DelegateExecution delegateExecution) {

        // 通过Spring上下文获取Service实例
        ICarsService iCarsService = (ICarsService) SpringUtils.getBean("carsServiceImpl");

        //回写状态 为 已审核  或 未审核
        Object id = delegateExecution.getVariable("carId");
        Object approvalState1 = delegateExecution.getVariable("approvalState1");

        Object approvalState2 = delegateExecution.getVariable("approvalState2");

        if (!ObjectUtils.isEmpty(approvalState1) && ObjectUtils.isEmpty(approvalState2)) {
            Cars cars = new Cars();
            cars.setId(Long.parseLong(id.toString()));
            cars.setCheckState(Long.parseLong(approvalState1.toString()));
            cars.setUpdateTime(DateUtils.getNowDate());
            cars.setUpdateUserId(ObjectUtils.isEmpty(SecurityUtils.getUserId()) ? 1 : SecurityUtils.getUserId() );
            cars.setUpdateUserName(ObjectUtils.isEmpty(SecurityUtils.getUsername()) ? "admin" : SecurityUtils.getUsername());
            iCarsService.updateCars(cars);
        }

        if (!ObjectUtils.isEmpty(approvalState1) && !ObjectUtils.isEmpty(approvalState2)) {
            Cars cars = new Cars();
            cars.setId(Long.parseLong(id.toString()));
            cars.setCheckState(Long.parseLong(approvalState2.toString()));
            cars.setUpdateTime(DateUtils.getNowDate());
            cars.setUpdateUserId(ObjectUtils.isEmpty(SecurityUtils.getUserId()) ? 1 : SecurityUtils.getUserId() );
            cars.setUpdateUserName(ObjectUtils.isEmpty(SecurityUtils.getUsername()) ? "admin" : SecurityUtils.getUsername());
            iCarsService.updateCars(cars);
        }

        //可以根据 最后一级谁更新的状态 来处理 审批人和审批时间

    }
}
